package com.srd.phone.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.shengrongdai.srd.R;
import com.srd.phone.utils.TextSizeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 退出的dialog  activity
 */
public class DialogListViewActivity extends AppCompatActivity {

    private ListView lv_dialog;

    private MyAdapter adapter;
    private List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        list = (List<String>) getIntent().getSerializableExtra("list");
        setContentView(R.layout.dialog_activity_listview);
        lv_dialog = (ListView) findViewById(R.id.lv_dialog);
        adapter = new MyAdapter();
        lv_dialog.setAdapter(adapter);
        lv_dialog.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra("index", position);
                setResult(position, intent);
                finish();
            }
        });

    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                TextView textView = new TextView(DialogListViewActivity.this);
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                textView.setTextColor(Color.BLACK);
                textView.setLayoutParams(new ListView.LayoutParams(-1, -2));
                textView.setPadding(TextSizeUtils.Dp2Px(DialogListViewActivity.this, 15), TextSizeUtils.Dp2Px(DialogListViewActivity.this, 12), 0, TextSizeUtils.Dp2Px(DialogListViewActivity.this, 12));
                textView.setText(list.get(position));
                convertView = textView;
            } else {
                TextView textView = (TextView) convertView;
                textView.setText(list.get(position));
            }
            return convertView;
        }
    }
}
